package com.opal.system.service.impl;

import com.opal.consts.WSC;
import com.opal.domain.R;
import com.opal.domain.ReturnMessageData;
import com.opal.system.entity.table.*;
import com.opal.system.mapper.mysql.OpalRoleMapper;
import com.opal.system.mapper.mysql.OpalRoleMapperExtend;
import com.opal.system.mapper.mysql.OpalUserRoleMapper;
import com.opal.system.service.OpalPermissionService;
import com.opal.system.service.OpalRoleService;
import org.apache.tomcat.websocket.WsContainerProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
public class OpalRoleServiceImpl implements OpalRoleService{
	
	@Autowired
	private OpalRoleMapperExtend opalRoleMapperExtend;
	@Autowired
	private OpalRoleMapper opalRoleMapper;
	@Autowired
	private OpalPermissionService opalPermissionService;
	@Autowired
	private OpalUserRoleMapper opalUserRoleMapper;

	@Override
	public List<OpalRole> selectAllrole() {
		OpalRoleExample ore = new OpalRoleExample();
		return opalRoleMapper.selectByExample(ore);
	}

	@Override
	public R updateRoleService(OpalRole opalRole) {
		try {
			int i = opalRoleMapper.updateByPrimaryKeySelective(opalRole);
			if (i<0){
				return new R(WSC.PRECONDITIONFAILED,"修改失败");
			}
			return new R(WSC.RESTPONSE_OK,"修改成功");
		}catch (DuplicateKeyException e){
			return new R(WSC.PRECONDITIONFAILED,"修改失败:角色名已存在");
		}catch (Exception e){
			e.printStackTrace();
			return new R(WSC.REQUEST_ERROR,"修改失败：出现异常");
		}

	}

	@Override
	public R addRoleService(OpalRole opalRole) {

		try {
			int i = opalRoleMapper.insertSelective(opalRole);
			if (i<0){
				return new R(WSC.PRECONDITIONFAILED,"添加失败");
			}
			return new R(WSC.RESTPONSE_OK,"添加成功");
		}catch (DuplicateKeyException e){
			return new R(WSC.PRECONDITIONFAILED,"添加失败:角色名已存在");
		}catch (Exception e){
			e.printStackTrace();
			return new R(WSC.REQUEST_ERROR,"添加失败：出现异常");
		}
	}

	@Override
	public List<OpalRole> getByUserIdRoles(OpalUser user) {
		List<OpalRole> listOpalRole= opalRoleMapperExtend.getByUserIdRolesMapper(user);
		return listOpalRole;
	}

	@Override
	@Transactional("mysqlTransactionManager")
	public ReturnMessageData deleteRoleService(OpalRole[] opalRole) {
		OpalUserRoleExample opalUserRoleExample = new OpalUserRoleExample();
		try {
			for (OpalRole or : opalRole) {
				//先删除用户和角色中的关联:多对多
				opalUserRoleExample.createCriteria().andRoleIdEqualTo(or.getRoleId());
				opalUserRoleMapper.deleteByExample(opalUserRoleExample);
				//修改权限中对应的角色id为null 一对多
				opalPermissionService.updateOpalPermissionByRoleId(or.getRoleId());
				//删除角色信息
				opalRoleMapper.deleteByPrimaryKey(or.getRoleId());
			}
			return new ReturnMessageData(WSC.RESTPONSE_OK, "删除成功");
		}catch (Exception e){
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return new ReturnMessageData(WSC.RESTPONSE_OK, "删除中出现异常");
		}

	}

	@Override
	public Object selectPermissionService() {
		List<OpalPermission> opalPermissions = opalPermissionService.selectOpalPermission();

		return opalPermissions;
	}

	@Override
	@Transactional("mysqlTransactionManager")
	public Object updatePermissionRoleIdService(List<Long> permisssIds, Long roleId) {
		try{
			//先清空(修改)该角色的所有权限
			opalPermissionService.updateOpalPermissionByRoleId(roleId);

			OpalPermission opalPermission =new OpalPermission();
			opalPermission.setRoleId(roleId);   //赋值角色id
			for (Long id:permisssIds){
				opalPermission.setPermissionId(id);//赋值条件：权限id
				opalPermissionService.updateOpalPermissionById(opalPermission);//根据主键id修改
			}
		}catch (Exception e){
			e.printStackTrace();
			return  new R(WSC.REQUEST_ERROR,"分配失败：修改中出现异常");
		}
		return new R(WSC.RESTPONSE_OK,"分配成功");
	}

	@Override
	public List<OpalPermission> selectPermissionByRoleIdservice(Long roleId) {
		OpalPermission opalPermission = new OpalPermission();
		opalPermission.setRoleId(roleId);
		return opalPermissionService.selectOpalPermissionByCondition(opalPermission);
	}


}
